package com.red.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.red.basic.domain.RedCustomFolder;
import com.red.basic.domain.RedGoodsFolder;
import com.red.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.red.common.annotation.Log;
import com.red.common.core.controller.BaseController;
import com.red.common.core.domain.AjaxResult;
import com.red.common.enums.BusinessType;
import com.red.basic.domain.RedGoods;
import com.red.basic.service.IRedGoodsService;
import com.red.common.utils.poi.ExcelUtil;
import com.red.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物资管理Controller
 * 
 * @author bug2222
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/basic/goods")
public class RedGoodsController extends BaseController
{
    @Autowired
    private IRedGoodsService redGoodsService;


    @PreAuthorize("@ss.hasPermi('basic:goods:list')")
    @GetMapping("/goodsFolder")
    public AjaxResult list()
    {
        List<RedGoodsFolder> list = redGoodsService.getGoodsFolder();
        return success(list);
    }
    /**
     * 查询物资管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(RedGoods redGoods)
    {
        startPage();
        List<RedGoods> list = redGoodsService.selectRedGoodsList(redGoods);
        return getDataTable(list);
    }

    /**
     * 导出物资管理列表
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:export')")
    @Log(title = "物资管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RedGoods redGoods)
    {
        List<RedGoods> list = redGoodsService.selectRedGoodsList(redGoods);
        ExcelUtil<RedGoods> util = new ExcelUtil<RedGoods>(RedGoods.class);
        util.exportExcel(response, list, "物资管理数据");
    }
    /**
     * 导入物资管理列表
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RedGoods> util = new ExcelUtil<RedGoods>(RedGoods.class);
        List<RedGoods> goodsList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = redGoodsService.importGoods(goodsList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<RedGoods> util = new ExcelUtil<RedGoods>(RedGoods.class);
        return util.importTemplateExcel("用户数据");
    }


    /**
     * 获取物资管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(redGoodsService.selectRedGoodsById(id));
    }

    /**
     * 新增物资管理
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:add')")
    @Log(title = "物资管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RedGoods redGoods)
    {
        return toAjax(redGoodsService.insertRedGoods(redGoods));
    }

    /**
     * 修改物资管理
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:edit')")
    @Log(title = "物资管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RedGoods redGoods)
    {
        return toAjax(redGoodsService.updateRedGoods(redGoods));
    }

    /**
     * 删除物资管理
     */
    @PreAuthorize("@ss.hasPermi('basic:goods:remove')")
    @Log(title = "物资管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(redGoodsService.deleteRedGoodsByIds(ids));
    }
}
